package com.cskaoyan.mall.service;

import com.cskaoyan.mall.bean.Goods;
import com.cskaoyan.mall.bean.GoodsExample;
import com.cskaoyan.mall.bean.bo.*;
import com.cskaoyan.mall.bean.vo.*;
import com.cskaoyan.mall.mapper.GoodsAttributeMapper;
import com.cskaoyan.mall.mapper.GoodsMapper;
import com.cskaoyan.mall.mapper.GoodsProductMapper;
import com.cskaoyan.mall.mapper.GoodsSpecificationMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Zsp
 * 2021/1/9
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;


    @Override
    public Integer getGoodsTotal() {
        return goodsMapper.selectAll();
    }

    @Override
    public GoodsListVO getGoodsList(Integer page, Integer limit,String goodsSn,String name, String sort, String order) {
        PageHelper.startPage(page,limit);
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria().andDeletedEqualTo(false);
        if (goodsSn != null){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (name != null){
            criteria.andNameLike("%" + name + "%");
        }
        goodsExample.setOrderByClause(sort + " " + order);
        List<GoodsItemsVO> goods = goodsMapper.selectByExample(goodsExample);
        Integer total = goodsMapper.selectAll();
        GoodsListVO goodsListVO = new GoodsListVO(total, goods);
        return goodsListVO;
    }

    /**
     * @WhereUse  逻辑删除goods表
     * @param goodsItemsVO
     * @return
     */
    @Override
    public int deleteGoods(GoodsItemsVO goodsItemsVO) {
        Goods goods = new Goods(); //用来指定删除的Goods对象
        goods.setId(goodsItemsVO.getId());
        goods.setDeleted(true);
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public GoodsDetailVO getGoodsDetail(Integer id) {
        List<Integer> categoryIds = goodsMapper.selectCategoryIds(id);
        GoodsItemsVO goodsItemsVO = goodsMapper.selectGoodsDetailByPrimaryKey(id);
        List<GoodsAttributesVO> attributesVOS =  goodsAttributeMapper.selectGoodsAttributeByGoodsId(id);
        List<GoodsSpecificationsVO> specificationsVOS = goodsSpecificationMapper.selectGoodsSepcByGoodsId(id);
        List<GoodsProductVO> goodsProductVOS = goodsProductMapper.selectGoodsProductByGoodsId(id);
        GoodsDetailVO goodsDetailVO = new GoodsDetailVO(categoryIds, goodsItemsVO, attributesVOS, specificationsVOS, goodsProductVOS);
        return goodsDetailVO;
    }

    /**
     * @WhereUse 后台商品信息的修改
     *           这里的更新的返回值可以进一步处理
     * @param goodsUpdateBO
     * @return
     */
    @Override
    public int updateGoods(GoodsUpdateBO goodsUpdateBO) {
        List<GoodsSpecificationsBO> specificationsBOS = goodsUpdateBO.getSpecifications();
        for (GoodsSpecificationsBO specificationsBO : specificationsBOS) {
            goodsSpecificationMapper.updateByGoodsSpecBOSelective(specificationsBO); //更新spec
        }
        List<GoodsProductsBO> goodsProductsBOS = goodsUpdateBO.getProducts();
        for (GoodsProductsBO goodsProductsBO : goodsProductsBOS) {
            goodsProductMapper.updateByGoodsProductBOSelective(goodsProductsBO); //更新GoodsProd
        }
        List<GoodsAttributesBO> attributesBOS = goodsUpdateBO.getAttributes();
        for (GoodsAttributesBO attributesBO : attributesBOS) {
            goodsAttributeMapper.updateByGoodsAttributeBOSeletive(attributesBO); //更新GoodsAttr
        }
        /*当前仅根据更新goods表的信息条目类判断更新的成功与否*/
        int update = goodsMapper.updateByGoodsBOSelective(goodsUpdateBO.getGoods());//更新goods
        return update;
    }

    /**
     * @WhereUse 后台商品新增
     * @param goodsCreateBO
     * @return
     */
    @Override
    public int insertGoods(GoodsCreateBO goodsCreateBO) {
        GoodsLite4CreateBO goods = goodsCreateBO.getGoods();

        int insert = goodsMapper.insertCreateGoodsSelective(goods);
        List<GoodsCreateBO.GoodsSpecLite4CreateBO> specLite4CreateBOS = goodsCreateBO.getSpecifications();
        for (GoodsCreateBO.GoodsSpecLite4CreateBO specLite4CreateBO : specLite4CreateBOS) {
            specLite4CreateBO.setGoodsId(goods.getGoodsId());
            int specInsert = goodsSpecificationMapper.insertCreateGoodsSpec(specLite4CreateBO);
        }
        List<GoodsCreateBO.GoodsProdLite4CreatedBO> prodLite4CreatedBOS = goodsCreateBO.getProducts();
        for (GoodsCreateBO.GoodsProdLite4CreatedBO prodLite4CreatedBO : prodLite4CreatedBOS) {
            prodLite4CreatedBO.setGoodsId(goods.getGoodsId());
            int prodInsert = goodsProductMapper.insertCreateGoodsProd(prodLite4CreatedBO);
        }
        List<GoodsCreateBO.GoodsAttrLite4CreateBO> attrLite4CreateBOS = goodsCreateBO.getAttributes();
        for (GoodsCreateBO.GoodsAttrLite4CreateBO attrLite4CreateBO : attrLite4CreateBOS) {
            attrLite4CreateBO.setGoodsId(goods.getGoodsId());
            int attrInsert = goodsAttributeMapper.insertCreateGoodsAttr(attrLite4CreateBO);
        }
        return insert;
    }

}
